﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework;

namespace LGE
{
    public static class Audio
    {
        public static AudioEngine audioEngine = null;
        public static WaveBank waveBank = null;
        public static SoundBank soundBank = null;

        static List<Cue> cues = new List<Cue>();

        // Apenas uma música pode tocar ao mesmo tempo
        public static Cue bgmCue = null;

        public static void Init(String audioEnginePath, String waveBankPath, String soundBankPath)
        {
            audioEngine = new AudioEngine(audioEnginePath);
            try { waveBank = new WaveBank(audioEngine, waveBankPath); }
            catch { waveBank = new WaveBank(audioEngine, waveBankPath, 0, 4); }
            soundBank = new SoundBank(audioEngine, soundBankPath);
        }

        public static void Update(GameTime gameTime)
        {
            for (int i = 0; i < cues.Count; ++i) if (cues[i].IsStopped) { cues[i].Dispose(); cues.RemoveAt(i); }
            audioEngine.Update();
        }

        public static void PlayBGM(string name)
        {
            if (bgmCue != null) bgmCue.Stop(AudioStopOptions.Immediate);
            bgmCue = soundBank.GetCue(name);
            bgmCue.Play();
        }

        public static bool PlayCue(string sfx)
        {
            try
            {
                Cue cue = soundBank.GetCue(sfx);
                cues.Add(cue);
                cue.Play();
            }
            catch { return false; }
            return true;
        }

        public static void DisposeAll()
        {
            cues.ForEach(c => c.Dispose());
        }

        public static Cue GetCue(string sfx)
        {
            return soundBank.GetCue(sfx);
        }
    }
}

namespace LGE.Old
{
    public static class Audio
    {
        static Dictionary<string, SoundEffect> SoundEffects;
        static Dictionary<string, Song> Songs;

        static Audio()
        {
            SoundEffects = new Dictionary<string, SoundEffect>();
            Songs = new Dictionary<string, Song>();
        }

        public static void AddSFX(string sfx)
        {
            if (SoundEffects.ContainsKey(sfx)) SoundEffects.Remove(sfx);
            SoundEffects.Add(sfx, Engine.Load<SoundEffect>(sfx));
        }

        public static bool PlaySFX(string sfx)
        {
            try { SoundEffects[sfx].Play(); }
            catch { return false; }
            return true;
        }

        public static bool PlaySong(string song)
        {
            if (!Songs.ContainsKey(song))
                Songs.Add(song, Engine.Load<Song>(song));
            try { MediaPlayer.Play(Songs[song]); }
            catch { return false; }
            return true;
        }

        public static void StopSong()
        {
            MediaPlayer.Stop();
        }

        public static void ToggleMute()
        {
            MediaPlayer.IsMuted = !MediaPlayer.IsMuted;
            SoundEffect.MasterVolume = MediaPlayer.IsMuted ? 0 : 1;
        }

        public static SoundEffectInstance GetSFXInstance(string sfx)
        {
            return SoundEffects[sfx].CreateInstance();
        }
    }
}
